let { intentM } = require( '../db' );
let { INTENT_STATE } = require( '../bussiness/config' );
let {SUCCESS_PAY } = INTENT_STATE;

const calMoneyFromDb = () => {


};

const getCurMoney = ( req, res ) => {
  intentM.get()
    .aggregate( [
      // 筛选所有支付成功的状态的记录
      {
        $match: { state: SUCCESS_PAY }
      },
      {
        $group: {
          // 计算所有的documents
          _id: null,
          totalValue: { $sum: "$total_fee" },
        }
      }
    ] )
    .toArray()
    .then( arr => {
      let document = arr[0] || {};
      let { totalValue } = document;
      res.json( { money:  totalValue } );
    });
};

module.exports = getCurMoney;